package dbutils.dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.SQLException;

//dbutils增删改
public class Demo1 {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner qr = new QueryRunner(dataSource);

    @Test
    public void insertUser(){
        String sql = "insert into user values(?,?,?)";
        try {
            qr.update(sql,4,"张越学","8468");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void insertUser1() throws SQLException {
        String sql="insert into user values(?,?,?)";
        Object[] arr={4,"张原雪","0199"};
        String s = qr.update(sql, arr) > 0 ? "添加成功" : "添加失败";
        System.out.println(s);
    }
    @Test
    public void updateUser(){
        String sql = "update user set username=? where id =?";
        try {
            qr.update(sql,"张原雪",4);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void deleteUser(){
        String sql ="delete from user where username =?";
        try {
            qr.update(sql,"张原雪");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
